import { Engine } from '@babylonjs/core/Engines/engine';
import { Stage } from '../../base/stage';
import { UINode } from './ui-node';
import { UIRoot } from './ui-root';
import { Utils } from '../../tool/utils';


/**
 * ui容器参数对象
 * @param name 名称
 * @param key 键名
 * @param display 是否显示
 */
interface IUIContainerOptions {
    name?: string,
    key?: string,
    display?: boolean,
}


/**
 * ui容器
 */
class UIContainer {

    private _display!: boolean;

    protected nodes: UINode[];
    protected root: UIRoot;
    protected style: IUIContainerOptions;

    public readonly id = Utils.id;
    public readonly name: string;
    public readonly key: string;

    constructor(engine: Engine, stage: Stage, root: UIRoot, options?: IUIContainerOptions) {
        this.root = root;
        const ops = options || {};
        this.name = ops.name || ('ui-container' + Utils.id);
        this.key = ops.key || this.name;
        this.style = this.initializeStyle(ops);
        this.nodes = this.initializeUINodes(engine, stage, this.style);
        this.display = ops.display === undefined ? true : ops.display;
    }

    /**
     * 设置尺寸比
     */
    public set sizeRatio(ratio: number) {
        this.nodes.forEach(node => {
            if (node.lockToCamera) node.size = ratio;
        });
    }

    /**
     * 设置是否显示
     */
    public set display(display: boolean) {
        this._display = display;
        this.nodes.forEach(node => node.display = display);
    }

    /**
     * 获取是否是显示的
     */
    public get display(): boolean {
        return this._display;
    }

    /**
     * 窗口大小改变时的响应函数
     */
    public resize(): void {
        this.nodes.forEach(node => {
            node.resize();
        });
    }

    /**
     * 初始化参数
     * @param options 输入的参数对象
     * @returns 处理后的参数对象
     */
    protected initializeStyle(options: IUIContainerOptions): IUIContainerOptions {
        return options;
    }

    /**
     * 初始化ui节点
     * @param engine 引擎
     * @param stage 所属舞台
     * @param style 参数对象
     * @returns ui节点
     */
    protected initializeUINodes(engine: Engine, stage: Stage, style: IUIContainerOptions): UINode[] {
        return [];
    }

}


export { UIContainer };
export type { IUIContainerOptions };